﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Cwt;

namespace WebApp.Management._Article
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid articleId = new Guid(Request.QueryString["id"]);
                ICwtService service = Global.Get<ICwtService>();
                Article article = service.FindArticle(articleId);
                if (article != null)
                {
                    this._ctlListCategory.DataSource = service.FindCategories();
                    this._ctlListCategory.DataBind();
                    this._ctlListCategory.SelectedValue = article.CategoryId.ToString();

                    this._ctlId.Value = articleId.ToString("N");
                    this._ctlName.Value = article.Title;
                    this._ctlPostedDate.Value = article.PostedDate.ToString("yyyy-MM-dd");
                    this._ctlAuthor.Value = article.Author;
                    this._ctlTopPoint.Value = article.TopPoint.ToString();
                    this._ctlVisitPoint.Value = article.VisitPoint.ToString();
                    this._ctlSummary.Value = article.Summary;
                    this._ctlContent.Value = article.Content;
                    this._ctlAllowReplay.Value = Convert.ToString(article.IsAllowReplay);
                    this._ctlReplayVisible.Value = Convert.ToString(article.IsReplayVisible);
                    this._ctlEnabled.Value = Convert.ToString(article.IsEnabled);
                }
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Form["_ctlid"];
            string title = Request.Form["_ctlName"];
            string postedDate = Request.Form["_ctlposteddate"];
            string author = Request.Form["_ctlauthor"];
            int toppoint = Convert.ToInt32(Request.Form["_ctlTopPoint"]);
            int visitepoint = Convert.ToInt32(Request.Form["_ctlVisitpoint"]);
            string content = Request.Form["_ctlcontent"];
            string summary = Request.Form["_ctlsummary"];
            string allowReplay = Request.Form["_ctlallowreplay"];
            string replayVisible = Request.Form["_ctlreplayvisible"];
            string enabled = Request.Form["_ctlenabled"];
            Guid categoryId = new Guid(_ctlListCategory.SelectedValue);

            ICwtService service = Global.Get<ICwtService>();
            Article model = new Article();
            model.Id = new Guid(id);
            model.Title = title;
            model.CategoryId = categoryId;
            model.PostedDate = Convert.ToDateTime(postedDate);
            model.AuthorId = Guid.Empty;
            model.Author = author;
            model.TopPoint = toppoint;
            model.VisitPoint = visitepoint;
            model.Content = content;
            model.Summary = summary;
            model.IsAllowReplay = Convert.ToBoolean(allowReplay);
            model.IsReplayVisible = Convert.ToBoolean(replayVisible);
            model.IsEnabled = Convert.ToBoolean(enabled);

            if (service.SaveArticle(model))
            {
                Response.Redirect("./List.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx");
            }
        }
    }
}